/*
	play
	gameplay wrangling
*/

#include <stdlib.h>
#include <hge.h>
#include <hgesprite.h>
#include <hgeparticle.h>
#include <hgefont.h>
#include "splash.h"
#include "menu.h"
#include "play.h"
#include "ball.h"
#include "obstacle.h"
#include "level.h"
#include "winsplash.h"

extern HGE *hge;
extern HTEXTURE bgtex;
extern hgeSprite *bgspr;
Ball *ball;
Level *level;

HTEXTURE obtex;
HTEXTURE partex;
HEFFECT csnd;
HEFFECT msnd;

extern hgeFont *f_text;
hgeSprite *parspr;
hgeParticleSystem *par;

std::string levelName = "level";
int levelNumber=1;
int numberLevels;

//lives, if 0 game is over
int lives = 5;

std::string n2i(int i);

bool Play_Clean()
{
	hge->Texture_Free(bgtex);
	hge->Texture_Free(obtex);
	hge->Texture_Free(partex);
	hge->Effect_Free(csnd);
	hge->Effect_Free(msnd);
	delete ball;
	delete level;
	delete f_text;
	delete par;
	return true;
}

bool Play_Init()
{
	bgtex=hge->Texture_Load("resources/images/andromeda.jpg");
	obtex=hge->Texture_Load("resources/images/block.png");
	partex=hge->Texture_Load("resources/images/particles.png");
	csnd=hge->Effect_Load("resources/sounds/ballcollide.wav");
	f_text=new hgeFont("resources/fonts/text.fnt");
	hge->System_SetState(HGE_INIFILE, "serious.ini");

	if(!f_text|!bgtex|!obtex|!partex|!csnd) return false;

	bgspr=new hgeSprite(bgtex,0,0,800,600);
	bgspr->SetBlendMode(BLEND_COLORMUL| BLEND_ALPHABLEND | BLEND_NOZWRITE);

	//creating and setting up our particle system
	parspr = new hgeSprite(partex, 32, 32, 32, 32);
	parspr->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
	parspr->SetHotSpot(16, 16);
	par = new hgeParticleSystem("resources/peffects/particle3.psi", parspr);
	par->Fire();

	ball=new Ball();
	
	levelName += n2i(levelNumber);
	level=new Level(levelName);
	numberLevels= level->numLevels;
	//loading the music for the level
	std::string tmp_path = "resources/music/" + level->music;
	const char *music_path = tmp_path.c_str();
	msnd=hge->Effect_Load(music_path);//change these to strings.

	//playing the music for this level
	hge->Effect_PlayEx(msnd, 50, 0, 1.0, true);

	levelName = "level";

	return true;
}

bool Play_Frame()
{
	float dt=hge->Timer_GetDelta();
	if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
	if (ball->IsCaught()){
		if (hge->Input_GetKeyState(HGEK_UP))ball->Move(dt,-1);
		if (hge->Input_GetKeyState(HGEK_DOWN))ball->Move(dt,1);
		if (hge->Input_GetKeyState(HGEK_LEFT))ball->Rotate(dt,-1);
		if (hge->Input_GetKeyState(HGEK_RIGHT))ball->Rotate(dt,1);
		if (hge->Input_GetKeyState(HGEK_CTRL))ball->Shoot();
	}

	//updating particle system
	par->info.nEmission = (int)(ball->speed)*200;
	par->MoveTo(ball->x, ball->y);
	par->Update(dt);

	if (ball->Dying()){
		ball->Die();
		lives--;

		if(lives <= 0){ 
			levelNumber = 1; //reset the level counter so we start all over again
			lives = 5;
			Play_Clean();
			Menu_Init();
			hge->System_SetState(HGE_FRAMEFUNC, Menu_Frame);
			hge->System_SetState(HGE_RENDERFUNC, Menu_Render);
			return false;
		}
	}

	int collided=level->Collide(ball);

	//temp cheat key
	bool cheat = false;
	if(hge->Input_GetKeyState(HGEK_S)){
		collided=2;
		cheat = true;
	}
	
	//add extra lives
	if(hge->Input_GetKeyState(HGEK_F)){lives++;}

	//reload level
	if(hge->Input_GetKeyState(HGEK_R)){
		Play_Clean();
		Play_Init();
		return false;
	}

	//end temp cheat keys

	if (collided!=-1){
		//entering the control block for a successful goal hit
		if(ball->Won() || cheat == true){
			levelNumber++; //incrementing the level counter

			if(levelNumber > numberLevels){ //we've run out of levels, game over you win!
				levelNumber = 1; //reset the level counter so we start all over again
				lives = 5;
				Play_Clean();
				Winsplash_Init();
				hge->System_SetState(HGE_FRAMEFUNC, Winsplash_Frame);
				hge->System_SetState(HGE_RENDERFUNC, Winsplash_Render);
				return false;
			}
			else{
				//moving on to the next level
				Play_Clean();
				Play_Init();
				return false;
			}
		}
		//regular impact with a non-goal object
		else{
			hge->Effect_PlayEx(csnd, 100);
			//ball->Bounce(collided);
		}
	}

	ball->Movement();

	return false;
}

bool Play_Render()
{
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	bgspr->Render(0,0);
	par->Render();
	ball->Render();
	level->Render();
	f_text->printf(5, 20, HGETEXT_LEFT, "speed:%.3f\nlives:%d",ball->travel.Length(), lives);
	f_text->printf(5, 550, HGETEXT_LEFT, "level: %d", levelNumber);
	hge->Gfx_EndScene();

	return false;
}

std::string n2i(int i){
	std::string ret="";

	if (i==0) {
		ret="0";
		return ret;
	}

	bool bNeg=(i < 0);
	i=abs(i); 

	while (i!=0) {
		std::string save=ret;
		ret=('0'+(i%10));
		ret+=save; 
		i/=10;
	}

	if (bNeg)
		ret = "-" + ret;

	return ret;

}
